﻿using Cysharp.Threading.Tasks;
using uMVP;
using UnityEngine.UI;

namespace ExceptionModule
{
    public interface IExceptionMessageBox
    {
        string Hint { set; }
    }
    
    [PrefabPath("UI/Prefabs/ErrorMessageBox", "/Canvas")]
    public class ExceptionMessageBox : ViewBase, IExceptionMessageBox
    {
        private Button _closeButton;
        private Text _text;
        public override UniTask OnInitializeAsync()
        {
            _text = ViewGameObject.transform.Find("Text").GetComponent<Text>();
            _closeButton = ViewGameObject.transform.Find("CloseButton").GetComponent<Button>();
            _closeButton.onClick.AddListener(Destroy);
            
            return default;
        }

        public override void OnDestroy()
        {
            _closeButton.onClick.RemoveAllListeners();
        }

        public string Hint
        {
            set => _text.text = value;
        }
    }
}